import Vue from 'vue'
import App from './App.vue'
import router from '@/router/index'
import store from '@/store/store'
import '@/assets/css/index.css'
import '@/utils/directive'
import axios from '@/utils/axios'
import ElementUI from 'element-ui'
import '@/assets/css/element-variables.scss'
import VModal from 'vue-js-modal'
import VueQuillEditor from 'vue-quill-editor'

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

// 时间格式化函数
Date.prototype.format = function(format) {
  var date = {
    'M+': this.getMonth() + 1,
    'd+': this.getDate(),
    'h+': this.getHours(),
    'm+': this.getMinutes(),
    's+': this.getSeconds(),
    'q+': Math.floor((this.getMonth() + 3) / 3),
    'S+': this.getMilliseconds()
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
        date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return format
}

Vue.filter('filterUrl', function(url) {
  let host = window.location.protocol === 'https' || window.location.hostname === 'ms.henyouliao.cn' ? 'https://ja-material-formal.oss-cn-shenzhen.aliyuncs.com' : 'https://ja-material-test.oss-cn-shenzhen.aliyuncs.com'
  if (!url) return
  if (url.indexOf('http') >= 0 || url.indexOf('https') >= 0) return { 'backgroundImage': `url(${url})` }
  else return { 'backgroundImage': `url(${host}${url})` }
})
Vue.filter('filterSrc', function(url) {
  let host = window.location.protocol === 'https' || window.location.hostname === 'ms.henyouliao.cn' ? 'https://ja-material-formal.oss-cn-shenzhen.aliyuncs.com' : 'https://ja-material-test.oss-cn-shenzhen.aliyuncs.com'
  if (!url) return
  if (url.indexOf('http') >= 0 || url.indexOf('https') >= 0) return url
  else return `${host}${url}`
})

Vue.filter('filterDate', function(date) {
  if (!date) return
  let time = parseInt(date.split('__DATE__')[1])
  return new Date(time).format('yyyy-MM-dd')
})

const messages = ['success', 'warning', 'info', 'error'];

messages.forEach(type => {
  ElementUI.Message[type] = options => {
    if (typeof options === 'string') {
      options = {
        message: options
      };
      // 默认配置
      options.duration = 1000;
      options.showClose = true;
    }
    options.type = type;
    return ElementUI.Message(options);
  };
})

Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.use(ElementUI)
Vue.use(VModal)
Vue.use(VueQuillEditor)

export default new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')